﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FF14TreasureDiggingTool.DataAccess.ApplicationDbContext;
using FF14TreasureDiggingTool.DataAccess.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Wpf.Ui.Common.Interfaces;
using Wpf.Ui.Controls.Interfaces;
using Wpf.Ui.Mvvm.Contracts;

namespace FF14TreasureDiggingTool.ViewModels;

public partial class SettingsViewModel : ObservableObject, INavigationAware
{
    private readonly ApplicationContext _dbContext;
    private readonly ISnackbarControl _snackbarControl;

    [ObservableProperty]
    private string _appVersion = String.Empty;

    [ObservableProperty]
    private Wpf.Ui.Appearance.ThemeType _currentTheme = Wpf.Ui.Appearance.ThemeType.Unknown;
    private bool _isInitialized = false;

    [ObservableProperty]
    private List<CustomRule> _rules;

    [ObservableProperty]
    private CustomRule _selectedRule;

    public SettingsViewModel(ApplicationContext dbContext, ISnackbarService snackbarService)
    {
        this._dbContext = dbContext;
        _snackbarControl = snackbarService.GetSnackbarControl();
    }

    [RelayCommand]
    public void ChangeCurrentRule()
    {
        if (SelectedRule is not null)
        {
            SelectedRule.IsCurrentRule = true;
            _dbContext.Update(SelectedRule);
            _dbContext.SaveChanges();

            _dbContext.CustomRules
                .Where(c => c.Id != SelectedRule.Id)
                .ExecuteUpdate(s => s.SetProperty(c => c.IsCurrentRule, c => false));

            _snackbarControl.Show("设置成功");
        }
    }

    public void OnNavigatedFrom() { }

    public void OnNavigatedTo()
    {
        if (_isInitialized)
        {
            Rules = _dbContext.CustomRules.ToList();
        }
        if (!_isInitialized)
            InitializeViewModel();
    }

    [RelayCommand]
    public void Switch()
    {
        Window window = Application.Current.MainWindow;
        window.Topmost = !window.Topmost;
    }

    private string GetAssemblyVersion()
    {
        return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version?.ToString()
            ?? String.Empty;
    }

    private void InitializeViewModel()
    {
        CurrentTheme = Wpf.Ui.Appearance.Theme.GetAppTheme();
        AppVersion = $"FF14TreasureDiggingTool - {GetAssemblyVersion()}";

        Rules = _dbContext.CustomRules.ToList();
        SelectedRule = Rules.FirstOrDefault(r => r.IsCurrentRule);
        if (SelectedRule is null)
        {
            SelectedRule = Rules.First();
        }
        _isInitialized = true;
    }

    [RelayCommand]
    private void OnChangeTheme(string parameter)
    {
        switch (parameter)
        {
            case "theme_light":
                if (CurrentTheme == Wpf.Ui.Appearance.ThemeType.Light)
                    break;

                Wpf.Ui.Appearance.Theme.Apply(Wpf.Ui.Appearance.ThemeType.Light);
                CurrentTheme = Wpf.Ui.Appearance.ThemeType.Light;

                break;

            default:
                if (CurrentTheme == Wpf.Ui.Appearance.ThemeType.Dark)
                    break;

                Wpf.Ui.Appearance.Theme.Apply(Wpf.Ui.Appearance.ThemeType.Dark);
                CurrentTheme = Wpf.Ui.Appearance.ThemeType.Dark;

                break;
        }
    }
}
